#include "mainwindow.h"
#include "ui_mainwindow.h"
// UOS记事本

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setUI();
}

void MainWindow::setUI(){
    codec = QTextCodec::codecForName("utf-8");
    //设置窗口大小以及初始位置
    this->setGeometry(200, 200, 800, 500);
    //设置窗口名称
    this->setWindowTitle(codec->toUnicode("Qt 记事本"));

    //主菜单
    mainMenu = new QMenuBar();
    //文件菜单
    fileMenu = new QMenu(codec->toUnicode("文件"));
    mainMenu->addMenu(fileMenu);

    //新建动作
    newAction = new QAction(QIcon(":/images/mynote/filenew"), codec->toUnicode("新建"));
    newAction->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_N));
    newAction->setStatusTip(tr("新建一个文件"));
    fileMenu->addAction(newAction);

    //打开动作
    openAction = new QAction(QIcon(":/images/mynote/fileopen"), codec->toUnicode("打开"));
    openAction->setShortcut(tr("Ctrl+O"));
    openAction->setStatusTip(tr("打开一个文件"));
    fileMenu->addAction(openAction);

    //保存动作
    saveAction = new QAction(QIcon(":/images/mynote/filesave"), codec->toUnicode("保存"));
    saveAction->setShortcut(tr("Ctrl+S"));
    saveAction->setStatusTip(tr("保存文件"));
    fileMenu->addAction(saveAction);

    //另存为动作
    saveAsAction = new QAction(QIcon(":/images/mynote/filesave"), codec->toUnicode("另存为"));
    saveAsAction->setShortcut(tr("Ctrl+Shift+S"));
    saveAsAction->setStatusTip(tr("另存为新的文件"));
    fileMenu->addAction(saveAsAction);

    //编辑菜单
    editMenu = new QMenu(codec->toUnicode("编辑"));
    mainMenu->addMenu(editMenu);

    //剪切动作
    cutAction = new QAction(QIcon(":/images/mynote/editcut"), codec->toUnicode("剪切"));
    cutAction->setShortcut(tr("Ctrl+X"));
    cutAction->setStatusTip(tr("剪切选中"));
    editMenu->addAction(cutAction);

    //复制动作
    copyAction = new QAction(QIcon(":/images/mynote/editcopy"), codec->toUnicode("复制"));
    copyAction->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_C));
    copyAction->setStatusTip(tr("复制选中"));
    editMenu->addAction(copyAction);

    //粘贴动作
    pasteAction = new QAction(QIcon(":/images/mynote/editpaste"), codec->toUnicode("粘贴"));
    pasteAction->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_V));
    pasteAction->setStatusTip(tr("粘贴粘贴板内容"));
    editMenu->addAction(pasteAction);

    //格式菜单
    formatMenu = new QMenu(codec->toUnicode("格式"));
    mainMenu->addMenu(formatMenu);

    //字体动作
    fontAction = new QAction(QIcon(":/images/mynote/font"),codec->toUnicode("字体"));
    fontAction->setStatusTip(tr("修改字体"));
    formatMenu->addAction(fontAction);

    //颜色动作
    colorAction = new QAction(codec->toUnicode("颜色"));
    colorAction->setStatusTip(tr("修改颜色"));
    formatMenu->addAction(colorAction);

    //关于菜单
    helpMenu = new QMenu(codec->toUnicode("帮助"));
    helpMenu->setStyleSheet("color:blue");
    mainMenu->addMenu(helpMenu);

    aboutAction = new QAction(codec->toUnicode("关于"));
    aboutAction->setShortcut(tr("Ctrl+H"));
    aboutAction->setStatusTip(tr("查看版本"));
    helpMenu->addAction(aboutAction);

    //主菜单添加至窗口
    this->setMenuBar(mainMenu);

    //将文本编辑器添加至窗口
    textEdit = new QTextEdit();
    this->setCentralWidget(textEdit);

    //工具栏
//    QToolBar *toolBar = addToolBar(tr("&File"));
    QToolBar *toolBar = new QToolBar(this);
    toolBar->setMovable(false);// 设置工具栏不可移动

    toolBar->addAction(openAction);
    toolBar->addAction(saveAction);
    toolBar->addAction(cutAction);
    toolBar->addAction(copyAction);
    toolBar->addAction(pasteAction);
    addToolBar(Qt::TopToolBarArea, toolBar);// 将工具栏添加到顶部

    //状态栏

    QLabel *labCellIndex = new QLabel("当前坐标: 0.0",this);
    labCellIndex->setMinimumWidth(150);

    QLabel *labCellType=new QLabel("文件编码: UTF-8",this);
    labCellType->setMinimumWidth(100);

    QLabel *labStudID=new QLabel("字数：0",this);
    labStudID->setMinimumWidth(100);

    // 将初始化的标签添加到底部状态栏上
    ui->statusbar->addWidget(labCellIndex);
    ui->statusbar->addWidget(labCellType);
    ui->statusbar->addWidget(labStudID);



    //链接信号与槽
    connect(newAction,SIGNAL(triggered()),this,SLOT(NewFile()));
    connect(openAction,SIGNAL(triggered()),this,SLOT(OpenFile()));
    connect(saveAction,SIGNAL(triggered()),this,SLOT(SaveFile()));
    connect(saveAsAction,SIGNAL(triggered()),this,SLOT(SaveAsFile()));
    connect(cutAction,SIGNAL(triggered()),this,SLOT(cut()));
    connect(copyAction,SIGNAL(triggered()),this,SLOT(copy()));
    connect(pasteAction, SIGNAL(triggered()), this, SLOT(paste()));
    connect(fontAction, SIGNAL(triggered()), this, SLOT(SetFont()));
    connect(colorAction, SIGNAL(triggered()), this, SLOT(SetColor()));
    connect(aboutAction, SIGNAL(triggered()), this, SLOT(About()));


}

void MainWindow::NewFile()
{
    currentFile.clear();
    textEdit->setText(QString());
}

void MainWindow::OpenFile()
{
    QString fileName = QFileDialog::getOpenFileName(this, "打开文件");
    QFile file(fileName);
    currentFile = fileName;
    if (!file.open(QIODevice::ReadOnly | QFile::Text)) {
        QMessageBox::warning(this, "警告", "无法打开文件: " + file.errorString());
        return;
    }
    setWindowTitle(fileName);
    QTextStream in(&file);
    in.setCodec("utf-8");
    QString text = in.readAll();
    textEdit->setText(text);
    file.close();
}

void MainWindow::SaveFile()
{
    QString fileName;
        // 若没有文件,重新创建一个
        if (currentFile.isEmpty()) {
            fileName = QFileDialog::getSaveFileName(this, "Save");
            currentFile = fileName;
        } else {
            fileName = currentFile;
        }
        QFile file(fileName);
        if (!file.open(QIODevice::WriteOnly | QFile::Text)) {
            QMessageBox::warning(this, "警告", "无法打开文件: " + file.errorString());
            return;
        }
        setWindowTitle(fileName);
        QTextStream out(&file);
        QString text = textEdit->toPlainText();
        out << text;
        file.close();
}

void MainWindow::SaveAsFile()
{
    QString fileName = QFileDialog::getSaveFileName(this, "另存为");
        QFile file(fileName);

        if (!file.open(QFile::WriteOnly | QFile::Text)) {
            QMessageBox::warning(this, "警告", "无法打开文件: " + file.errorString());
            return;
        }
        currentFile = fileName;
        setWindowTitle(fileName);
        QTextStream out(&file);
        QString text = textEdit->toPlainText();
        out << text;
        file.close();
}

void MainWindow::cut()
{
    textEdit->cut();
}

void MainWindow::copy()
{
    textEdit->copy();
}

void MainWindow::paste()
{
    textEdit->paste();
}

void MainWindow::SetFont()
{

}

void MainWindow::SetColor()
{

}

void MainWindow::About()
{

}

MainWindow::~MainWindow()
{
    delete ui;
}

